﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CMS.Web.UI;


using TRPOP.CMS.Business;
using TRPOP.CMS.Model;
using Newtonsoft.Json;
using System.Data;

namespace TRPOP.CMS.Web.Admin
{
    public partial class content_list : BaseAdmin
    {

        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_SubPagePermission = Plugin.PermissionKeys.PermissionEnum.Content_AllControl;
        }
        #endregion

        #region 页面变量
        protected bool m_IsAdd = true;
        /// <summary>
        /// 结点ID（绑定列表用）
        /// </summary>
        protected int m_intNodeId = 0;
        //发现以下变量没用，所以暂时注释掉
        //protected int m_intId = 0;
        //protected string m_strParentName = string.Empty;
        protected string m_strNodeName = string.Empty;
        protected NodeInfo m_NodeInfo = new NodeInfo();
        protected NodeManage m_NodeManage = new NodeManage();
        protected ContentManage m_ContentManage = new ContentManage();
        protected string m_strAction = string.Empty;
        protected int m_intRecordCount = 0;
        //发现此变量没用，所以暂时注释掉
        //private DataSet ds = new DataSet();
        /// <summary>
        /// 动态标题列 的List数据
        /// </summary>
        private List<ModelFieldInfo> TitleList = null;
        /// <summary>
        /// 动态标题列
        /// </summary>
        private string strListFields = string.Empty;
        /// <summary>
        /// 信息发布状态（绑定列表用）
        /// </summary>
        private int m_intPubStatus = 0;
        protected int m_intContentId = 0;
        /// <summary>
        /// 信息绑定时的每行数据 
        /// </summary>
        protected DataRowView dataRowView = null;
        /// <summary>
        /// 信息发布发布日期
        /// </summary>
        protected string m_strDateTime = string.Empty;
        /// <summary>
        /// 信息关键字
        /// </summary>
        protected string m_strKeyWords = string.Empty;
        #endregion

        #region 页面事件
        /// <summary>
        /// 页面Load事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            m_intNodeId = InitRequestViewState("nodeid").ToInt();// RequestHelper.RequestInt("nodeid");
            m_strAction = InitRequestViewState("ac");//RequestHelper.RequestString("ac");
            m_intPubStatus = InitRequestViewState("state").ToInt(100);// RequestHelper.RequestInt("state", 100);
            m_intContentId = InitRequestViewState("contentid").ToInt();  //RequestHelper.RequestInt("contentid");
            m_strDateTime = InitRequestViewState("Date");
            m_strKeyWords = InitRequestViewState("Keywords");
            if (!IsPostBack)
            {
                LoadBase();
                BindData();
            }
        }
        /// <summary>
        /// 信息列表（不包含头）的绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            dataRowView = (DataRowView)e.Item.DataItem;
            Repeater rep = (Repeater)e.Item.FindControl("rptFieldList");
            if (rep != null)
            {
                rep.DataSource = repTitleList.DataSource;
                rep.DataBind();
            }
        }
        #endregion

        #region 页面方法
        /// <summary>
        /// 第一次加载页面，所需绑定的数据
        /// </summary>
        public void LoadBase()
        {
            // 信息发布状态选项
            ddlPublishStatus.Items.Clear();
            ddlPublishStatus.Items.Add(new ListItem("所有文章", ""));
            ddlPublishStatus.Items.Add(new ListItem("所有未发布文章", ((int)PubStatus.EnumElement.UnPublish).ToString()));
            ddlPublishStatus.Items.Add(new ListItem("所有已发布文章", ((int)PubStatus.EnumElement.Publish).ToString()));
            //ddlPublishStatus.AutoPostBack = true;
            //ddlPublishStatus.SelectedIndexChanged += new System.EventHandler(this.ddlPublishStatus_SelectedIndexChanged);          
            

            //搜索的时间字段
            if (m_strDateTime.IsEmpty() == false)
                txtPubTime.Text = m_strDateTime;
            //搜索的关键字
            if (m_strKeyWords.IsEmpty() == false)
                SearchKeywords.Value = m_strKeyWords;

            #region 设置发布时间的相关控件
            DateTime dt = DateTime.Now;
            txtDate.Text = dt.ToString("yyyy-MM-dd");
            ddlHour.Items.Clear();
            for (int i = 0; i < 24; i++)
            {
                string s = i < 9 ? "0" + i.ToString() : i.ToString();
                ddlHour.Items.Add(new ListItem(s, i.ToString()));
            }
            ddlHour.SelectedValue = dt.Hour.ToString();
            ddlMinute.Items.Clear();
            for (int i = 0; i < 60; i++)
            {
                string s = i < 9 ? "0" + i.ToString() : i.ToString();
                ddlMinute.Items.Add(new ListItem(s, i.ToString()));
            }
            ddlMinute.SelectedValue = dt.Minute.ToString();
            ddlSecond.Items.Clear();
            for (int i = 0; i < 60; i++)
            {
                string s = i < 9 ? "0" + i.ToString() : i.ToString();
                ddlSecond.Items.Add(new ListItem(s, i.ToString()));
            }
            ddlSecond.SelectedValue = dt.Second.ToString();
            #endregion

            #region 动态标题列 的加载
            TitleList = (new ModelFieldManage()).GetTableFieldListDisplayListByNodeId(m_intNodeId);
            repTitleList.DataSource = TitleList;
            repTitleList.DataBind();
            if (TitleList != null)
            {
                foreach (ModelFieldInfo oInfo in TitleList)
                {
                    strListFields += "," + oInfo.FieldName;
                }
            }
            /* 原来的代码有问题，已替换为上面的代码
            for (int i = 0; i < TitleList.Count; i++)
            {
                strListFields += "," + TitleList[i].FieldName;
            }
            */
            #endregion

        }

        /// <summary>
        ///  绑定信息列表
        /// </summary>
        public void BindData()
        {
            if (m_intNodeId > 0 && (m_NodeInfo = m_NodeManage.GetInfoById(m_intNodeId)) != null && m_NodeInfo.NodeId > 0)
            {
                m_strNodeName = "[" + m_NodeInfo.Name + "]";
                Pager.PageSize = RequestHelper.RequestInt("offset", SYSConfig.SystemVar_AdminContentPageNum);
                if (Pager.PageSize > SYSConfig.SystemVar_PubPageListMax)
                {
                    Pager.PageSize = SYSConfig.SystemVar_PubPageListMax;
                }
                string strPubStatus = string.Empty;
                string strContent = string.Empty;
                string strDate = string.Empty;
                string strKeywords = string.Empty;
                switch (m_intPubStatus)
                {
                    case 0:
                    case 1:
                        strPubStatus = " and PubStatus=" + m_intPubStatus;
                        break;
                    default:
                        strPubStatus = " and PubStatus >=0 ";
                        break;
                }
                if (m_intContentId > 0)
                {
                    strContent = " and ContentId=" + m_intContentId + " ";
                }
                if (m_strDateTime.IsEmpty() == false)
                {
                    DateTime dtTime = m_strDateTime.ToDateTime();
                    if (dtTime != StringExtension.NullDateTime)
                    {
                        strDate = " and PubTime>='" + dtTime.ToString() + "' and PubTime <'" + dtTime.AddDays(1).ToString() + "' ";
                    }
                }
                if (m_strKeyWords.IsEmpty() == false)
                {
                    strKeywords = m_ContentManage.GetExtendIdBySearchKeySQL(m_strKeyWords, m_intNodeId);
                    if (strKeywords.IsEmpty() == false)
                    {
                        strKeywords = " and ExtendId in (" + strKeywords + ")";
                    }
                }
                if (Pager.PageSize <= 0) { Pager.PageSize = SYSConfig.SystemVar_AdminContentPageNum; }
                rptList.DataSource = (new ContentManage()).GetContentListByPager(m_intNodeId, Pager.CurrentPage, Pager.PageSize, strContent + strPubStatus + "  and NodeId=" + m_intNodeId + strDate + strKeywords, BaseAdmin.m_ContentDisplayOrderBY, out m_intRecordCount);
                rptList.DataBind();
                dataRowView = null;
                Pager.ShowRecordCount = true;
                Pager.ShowPageSize = true;
                Pager.ShowCurrentPage = true;
                Pager.RecordCount = m_intRecordCount;
            }
        }
        #endregion
    }
}